<?php

namespace pmvc\ioc\xml;

use pmvc\util\ClassUtil;

use pmvc\util\XMLUtil;
use pmvc\util\FileUtil;

use pmvc\ioc\PropertyDescriptor;
use pmvc\ioc\ObjectDescriptor;

use DOMElement;
use Exception;

/**
 * Allows for the importing of other XML contexts.
 *
 */
class ImportExtension
	implements Extension {

	/**
	 * {@inheritDoc}
	 */
	public function process(DOMElement $element, ParseContext $pc) {

		// get the file
		$file = XMLUtil::getAttributeValue($element, "file");
		if (!isset($file)) {
			throw new Exception("File must be specified and must exist");
		}

		// check relative path
		$loc = FileUtil::getRelativePath($file, $pc->getXmlFile());
		if ($loc===false) {
			throw new Exception("Cant find file or resource: ".$file." referenced from ".$pc->getXmlFile());
		}

		// add the file
		$pc->getObjectFactory()->addXmlFile($loc);
	}
}